package test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.lucene.queryparser.classic.ParseException;
import org.junit.Test;

import database.Jdbcmysql;
import searchprocessing.aggregate.AggregateMatchUnit;
import searchprocessing.exactmatch.ExactMatchUnit;
import searchprocessing.similaritymatch.SimilarMatchUnit;
import structure.EdgeMatchpair;
import structure.Relationship;
import assetprocess.AssetPreprocess;
import assetprocess.ReadFile;
/**
 * 程式資訊摘要 : 測試主程式
 * 版本資訊:1.0
 * 程式設計人員姓名: 朱聖池
 */
public class testWork_AdvancedSearch {
    private String assesXmlFileDirPath = "D:/AssetsOld/";
    private String queryXmlFileDirPath = "D:/Queries/";
    private String filePath = "D:/AssetContent/" ;
    private HashMap<String,ArrayList<EdgeMatchpair>> similarEdge = new HashMap<String,ArrayList<EdgeMatchpair>>();
    private HashMap<String,HashMap<String,ArrayList<Relationship>>> data_of_headMap = 
			new HashMap<String,HashMap<String,ArrayList<Relationship>>>();
    /**
     * 主程式
     * @throws IOException
     * @throws ParseException
     */


    @Test
	public void main() throws IOException, ParseException {
		//testInsertFeature();
		testExactMatch();
		//testSimilarMatch();
		//testAggregateSearch();
    	//updateGraph();
    	//deletGraph();
	}
  
    /**
     * 刪除Graph特徵
     */
	public void deletGraph() {
		String id = "";
		String version = "";
		Jdbcmysql conf = new Jdbcmysql();
		conf.deleteGraphID(id,version);
		
	}


	/**
     * 擷取檔案資料並存入DB
	 * @throws IOException 
     */
    @Test
    public void testInsertFeature() throws IOException {
        // 設定讀取智產檔案路徑，並將資料夾內檔案路徑儲存
        ReadFile readXML = new ReadFile();
        readXML.setDirPath(assesXmlFileDirPath); // 檔案路徑
        // 讀取資料夾內的全部檔案
        String[] FilePath = readXML.doReadFile();

        AssetPreprocess assetInsertProcess = new AssetPreprocess();

        //String filepath = FilePath[0] ;
        for (String filepath : FilePath) {
            // 設定讀取檔案
            assetInsertProcess.setAssetInformation(filepath);
            assetInsertProcess.insertDB();
            assetInsertProcess.resetGolbal(); //用來重設參數
        }
    

    }
    /**
     * 完全比對
     * @throws IOException 
     * @throws ParseException 
     */
    @Test
    public void testExactMatch() throws IOException, ParseException {
    	// 設定讀取Query檔案路徑，並將資料夾內檔案路徑儲存
    	ReadFile readXML = new ReadFile();
        readXML.setDirPath(queryXmlFileDirPath);
        
        // 讀取Query資料夾內的全部檔案
        String FilePath = queryXmlFileDirPath + "queryXML04.xml/" ;
        
    	AssetPreprocess QueryAsset = new AssetPreprocess();
    	QueryAsset.setAssetInformation(FilePath);
		
    	ExactMatchUnit QuerySearch = new ExactMatchUnit(QueryAsset.getNode(),QueryAsset.getEdge());
    	QuerySearch.matched();
	}
    
    /**
     * 相似比對
     * @throws IOException 
     * @throws ParseException 
     */
    @Test
    public void testSimilarMatch() throws IOException, ParseException {
    	// 設定讀取Query檔案路徑，並將資料夾內檔案路徑儲存
    	ReadFile readXML = new ReadFile();
        readXML.setDirPath(queryXmlFileDirPath);
        
        // 讀取Query資料夾內的全部檔案
        String FilePath = queryXmlFileDirPath + "queryXML06.xml/" ;
        
    	AssetPreprocess QueryAsset = new AssetPreprocess();
    	QueryAsset.setAssetInformation(FilePath);
    	
    	SimilarMatchUnit QuerySearch = new SimilarMatchUnit(QueryAsset.getNode(),QueryAsset.getEdge());
    	QuerySearch.matched();
    	//
    	
    	
	}    
    /**
     * 從相似比對的資料找出如何組合才可以最佳
     * @throws IOException 
     * @throws ParseException 
     */
    @Test
	public void testAggregateSearch() throws IOException, ParseException{
    	// 設定讀取Query檔案路徑，並將資料夾內檔案路徑儲存
    	ReadFile readXML = new ReadFile();
        readXML.setDirPath(queryXmlFileDirPath);
        
        // 讀取Query資料夾內的全部檔案
        String FilePath = queryXmlFileDirPath + "queryXML07.xml/" ;
        
    	AssetPreprocess QueryAsset = new AssetPreprocess();
    	QueryAsset.setAssetInformation(FilePath);
    	
    	SimilarMatchUnit QuerySearch = new SimilarMatchUnit(QueryAsset.getNode(),QueryAsset.getEdge());
    	QuerySearch.matched();
    	
    	
    	
    	AggregateMatchUnit optima = new AggregateMatchUnit(QuerySearch.getNode(),QuerySearch.getEdge(),
    			QuerySearch.getSolution_EdgeMatchTable(),QuerySearch.getSolution_NodeMatchTable()
    			,QuerySearch.changeArray(QuerySearch.getQueryMatchTable()));
    	optima.matched();
    }
    /**
     * 刪除已有的資料，重新新增update
     * @throws IOException
     */
    @Test
    public void updateGraph() throws IOException{
    	// 設定讀取智產檔案路徑，並將資料夾內檔案路徑儲存
        ReadFile readXML = new ReadFile();
        readXML.setDirPath(assesXmlFileDirPath); // 檔案路徑
        // 讀取資料夾內的全部檔案
        String[] FilePath = readXML.doReadFile();
        AssetPreprocess assetInsertProcess = new AssetPreprocess();
        
        for (String filepath : FilePath) {
            // 設定讀取檔案
            assetInsertProcess.setAssetInformation(filepath);
            Jdbcmysql conf = new Jdbcmysql();
            conf.deleteSolution(assetInsertProcess.getSolutionId()+"_"+assetInsertProcess.getSolutionVersion());
            conf.deleteNode(assetInsertProcess.getSolutionId());
            conf.deleteEdge(assetInsertProcess.getSolutionId());
            
            assetInsertProcess.insertDB();
            assetInsertProcess.resetGolbal(); //用來重設參數
        }
    }

}
